﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 01/03/2011
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoaspnet_Roles
    {
        private string className = "daoaspnet_Roles";
        public daoaspnet_Roles()
        {

        }
        private aspnet_Roles Load(IDataReader de)
        {
            aspnet_Roles item = new aspnet_Roles();
            item.ApplicationId = clsConvert.DbToGuid("ApplicationId", de);
            item.RoleId = clsConvert.DbToGuid("RoleId", de);
            item.RoleName = clsConvert.DbToString("RoleName", de);
            item.LoweredRoleName = clsConvert.DbToString("LoweredRoleName", de);
            item.Description = clsConvert.DbToString("Description", de);
            item.MenuFile = clsConvert.DbToString("MenuFile", de);
            item.Level = clsConvert.DbToInt("Level", de);
            return item;
        }

        public aspnet_Roles GetByName(string applicationName, string name)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            aspnet_Roles item = new aspnet_Roles();
            try
            {
                para[0] = new SqlParameter("ApplicationName", SqlDbType.NVarChar);
                para[0].Value = applicationName;
                para[1] = new SqlParameter(busaspnet_Roles.RoleName, SqlDbType.NVarChar);
                para[1].Value = name;
                using (de = clsSQL.SPDataReader("aspnet_Roles_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }
                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog(className, "GetByName(string applicationName,string name)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;

        }
        public int GetMaxRoleLevel(string applicationName, Guid userId)
        {
            string result = string.Empty;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            aspnet_Roles item = new aspnet_Roles();
            try
            {
                para[0] = new SqlParameter("ApplicationName", SqlDbType.NVarChar);
                para[0].Value = applicationName;
                para[1] = new SqlParameter("UserId", SqlDbType.UniqueIdentifier);
                para[1].Value = userId;
                using (de = clsSQL.SPDataReader("aspnet_Users_GetMaxRoleLevel", para))
                {
                    if (de.Read())
                    {
                        result = de.GetValue(0).ToString();
                    }
                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog(className, "GetMaxRoleLevel(string applicationName, Guid userId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return clsConvert.Int(result);
        }
        public aspnet_Roles GetMaxRole(string applicationName, Guid userId)
        {
            string result = string.Empty;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            SqlDataReader de;
            aspnet_Roles item = new aspnet_Roles();
            try
            {
                para[0] = new SqlParameter("ApplicationName", SqlDbType.NVarChar);
                para[0].Value = applicationName;
                para[1] = new SqlParameter("UserId", SqlDbType.UniqueIdentifier);
                para[1].Value = userId;
                using (de = clsSQL.SPDataReader("aspnet_Roles_GetMaxRole", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }
                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog(className, "GetMaxRole(string applicationName, Guid userId)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return item;
        }
    }
}

